package com.t3.tuio
{
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	public class TUIOObject extends TUIOPoint
	{
		protected var _f_id:uint;
		protected var _a:Number;
		protected var _da:Number;
		protected var _cursor:TUIOCursor;
		protected var _isActive:Boolean = true;
		protected var _persistent:Boolean = false;
		
		protected var _timer:Timer;
		
		public function TUIOObject( event:TUIOEvent, persistant:Boolean = false ):void
		{ 
			super( event );
			_persistent = persistant;
			this._f_id = event.f_id;
			
			_timer = new Timer( 1000, 1 );
			_timer.addEventListener( TimerEvent.TIMER_COMPLETE, keepAlive );
						
			repaint();
		}
		
		protected function repaint():void
		{
			this.graphics.beginFill( 0xFF0000 );
			this.graphics.drawRect( -25, -25, 50, 50 );
			this.graphics.endFill();
		}
		
		/* Als de fiducial opnieuw op de stage wordt gezet (ivm 'persistent' fiducials)*/
		public function addToStage():void
		{
			_timer.stop();
			_timer.reset();
			_isActive = true;
			this.visible = true;
		}
		
		public function removed():void
		{
			if( persistent )
			{
				_isActive = false;
				_timer.start();
			}
		}
		
		protected function keepAlive( event:TimerEvent ):void
		{
			if( !_isActive )
			{
				var tevent:TUIOEvent = new TUIOEvent( TUIOEvent.REMOVE_TUIO_OBJ );
				tevent.point = this;
				tevent.f_id = this.f_id;
				this.visible = false;
				
				dispatchEvent( tevent );
			}
		}
		
		public override function update( event:TUIOEvent, ignoreEvent:Boolean = false ):void
		{	
			_isActive = true;
			
			if( ignoreEvent || ( event.x != _x || event.y != _y || event.a != _a ) )
			{
				super.update( event );
				
				this.stageLoc = new Point( event.xStage, event.yStage );
				
				this._a = event.a;
				this._da = event.da;
			}
		}
		
		public function get persistent():Boolean
		{
			return _persistent;
		}
		
		public function get f_id():uint
		{
			return this._f_id;
		}
		
		public function get a():Number
		{
			return this._a;
		}
		
		public function get da():Number
		{
			return this._da;
		}
		
		public function get rot():Number
		{
			return this._a / 180 / Math.PI;
		}
		
		public function get isActive():Boolean
		{
			return _isActive;
		}
		
		public function set isActive( isActive:Boolean ):void
		{
			_isActive = isActive;
		}
	}
}